pro pfile2Grid,  gridname, spline=spline ;ext=ext,
;;re-writted by wuxk 2019-1-19


;  ON_ERROR, 2

; if ( size(pfile,/type) ne 7 ) then begin
;     print, "Wrong input pfile."
;     return
; endif

; if ( size(gridname,/type) ne 7 ) then begin
;     print, "Wrong input BOUT++ grid file."
;     return
; endif

  g=file_import(gridname)
; tmp=read_ascii(pfile)

  print,max(g.rxy[g.nx-1,*], ypeak), ypeak
  ixsep=g.ixseps1
;  if keyword_set(ext) then begin
;     ixsep = FIX(ixsep*1.05)
;  endif
  print,'Fix function is used to get the integer of value'
  print,'due to some foolish reasions, you have to input /spline'
  if keyword_set(spline) then begin
     print,'Using tension spline function to smooth.'
     xpls = intarr(9)
     xpls[0] = 0    ; set default x index
     xpls[8] = g.nx-1
     xpls[1] = FIX(g.nx*0.39)
     xpls[2] = FIX(g.nx*0.59)
     xpls[3] = FIX(g.nx*0.64)
     xpls[4] = FIX(g.nx*0.70)
     xpls[5] = FIX(g.nx*0.74)
     xpls[6] = FIX(g.nx*0.82)
     xpls[7] = FIX(g.nx*0.91) 
     tens = 5
     nodes = n_elements(xpls)
     help,xpls,tens,nodes
  endif

  if (ixsep lt 0. ) then begin
     ixsep = g.nx-1
  endif else begin
     if (ixsep ge g.nx) then begin
        ixsep = g.nx-1
     endif
  endelse 

  psn=(g.psixy[*,ypeak]-g.psi_axis)/(g.psi_bndry-g.psi_axis)
  psn2=psn[0:ixsep]

;;Interpolate ne
  ne_read=read_ascii("ne.txt")
  n_ne=n_elements(ne_read.field1[0,*])
  print,'Number of ne is: ', n_ne
  psn_ne=fltarr(n_ne)
  ne0=fltarr(n_ne)
  psn_ne=ne_read.field1[0,0:n_ne-1]
  ne0=ne_read.field1[1,0:n_ne-1]
  ne_tmp=interpol(ne0,psn_ne,psn,/spline)
  for i=0,ixsep do ne_tmp[i]=ne_tmp[i]
  for i=ixsep+1,g.nx-1 do ne_tmp[i]=ne_tmp[i-1]/1.01

;;Interpolate Ti 
  ti_read=read_ascii("ti.txt")
  n_ti=n_elements(ti_read.field1[0,*])
  print,'Number of ti is: ', n_ti
  psn_ti=fltarr(n_ti)
  ti0=fltarr(n_ti)
  psn_ti=ti_read.field1[0,0:n_ti-1]
  ti0=ti_read.field1[1,0:n_ti-1]
  ti_tmp=interpol(ti0,psn_ti,psn,/spline)
  for i=0,ixsep do ti_tmp[i]=ti_tmp[i]
  for i=ixsep+1,g.nx-1 do ti_tmp[i]=ti_tmp[i-1]/1.01
  ni_tmp=ne_tmp
  te_tmp=ti_tmp

;;Interpolate p0 
  p0 = g.pressure[*,ypeak]
  p0=ne_tmp*ti_tmp*16
  ;te_tmp=te_tmp*1e3
  ;ti_tmp=ti_tmp*1e3
;; smooth data
  safe_colors,/first
  if keyword_set(spline) then begin
     help,xpls,tens,nodes
     ;window,0,xsize=400,ysize=300,xpos=0,ypos=400
     ;pf=smspline(p0,tens,nodes,xpls,/plot)
     ;window,1,xsize=400,ysize=300,xpos=400,ypos=400
     ;nef=smspline(ne_tmp,tens,nodes,xpls,/plot)
     ;window,2,xsize=400,ysize=300,xpos=800,ypos=400
     ;tif=smspline(ti_tmp,tens,nodes,xpls,/plot)
     
     !p.multi=[0,3,1,0,0]
     window,0,xsize=1300,ysize=500,xpos=0,ypos=250
     pf=smspline(p0,tens,nodes,xpls,/plot)
     nef=smspline(ne_tmp,tens,nodes,xpls,/plot)
     tif=smspline(ti_tmp,tens,nodes,xpls,/plot)


     opt = get_yesno("Is this smooth OK?")
     if opt ne 1 then begin
        repeat begin
           print,"Using input x index for spline-nodes:" 
           nodes = get_integer("Please enter the total number of input x index:")
           xpls = intarr(nodes)
           xpls[0] = 0
           xpls[nodes-1] = g.nx-1
           print,"The x index for position 1 is set to be: ",xpls[0]
           for i =1, nodes-2 do begin
              print,"Please enter x index for position",i+1," :"
              xpls[i]=get_integer()
           endfor
           print,"The x index for the last position is set to be: ",xpls[nodes-1]
           ;window,0,xsize=400,ysize=300
           ;pf=smspline(p0,tens,nodes,xpls,/plot)
           ;window,1,xsize=400,ysize=300
           ;nef=smspline(ne_tmp,tens,nodes,xpls,/plot)
           ;window,2,xsize=400,ysize=300
           ;tif=smspline(ti_tmp,tens,nodes,xpls,/plot)
           
           !p.multi=[0,3,1,0,0]
           window,0,xsize=1300,ysize=500,xpos=0,ypos=250
           pf=smspline(p0,tens,nodes,xpls,/plot)
           nef=smspline(ne_tmp,tens,nodes,xpls,/plot)
           tif=smspline(ti_tmp,tens,nodes,xpls,/plot)
	   
           opt = get_yesno("Is this smooth OK?")
        endrep until opt eq 1
     endif

     p0 = pf
     nee = nef
     ni = nef
     ti = tif
     te = tif     
  endif    
;;Save data
  !p.multi=[0,3,1,0,0]
  !y.omargin=[2,10]
  window,0,xsize=1300,ysize=500,xpos=0,ypos=250
  plot,psn,p0,xtitle='X',title='p0 [P0]',chars=2.5
  plot,psn,ni,xtitle='X',title='Ni0[N0]',chars=2.5
  plot,psn,ti,xtitle='X',title='Ti0[T0]',chars=2.5
  xyouts,-0.1,8,'Please check the Units',chars=3.0
 
  save,nee,ne_tmp,f='ne.sav'
  save,te,te_tmp,f='te.sav'
  save,ni,ni_tmp,f='ni.sav'
  save,ti,ti_tmp,f='ti.sav'
  save,p0,f='p0.sav'
  
  

end

